package com.carb.config;

import com.carb.interceptor.JwtTokenAdminInterceptor;
import com.carb.interceptor.JwtTokenMemberInterceptor;
import com.carb.json.JacksonObjectMapper;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * 配置类，注册web层相关组件
 */
@Slf4j
@Configuration
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    @Resource
    private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;

    @Resource
    private JwtTokenMemberInterceptor jwtTokenMemberInterceptor;

    /**
     * 扩展Spring MVC框架的消息转化器
     * @param registry 格式化器注册
     */
    @Override
    protected void addFormatters(FormatterRegistry registry) {
        registry.addConverterFactory(new EnumConverter());
    }

    /**
     * 注册自定义拦截器
     * @param registry 拦截器注册
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");
        registry.addInterceptor(jwtTokenAdminInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/common/login");

        registry.addInterceptor(jwtTokenMemberInterceptor)
                .addPathPatterns("/member/**")
                .excludePathPatterns("/member/common/login")
                .excludePathPatterns("/member/common/register");
    }

    /**
     * 通过knife4j V4生成接口文档
     * @return OpenAPI
     */
    @Bean
    public OpenAPI openAPI() {
        log.info("开始生成接口文档...");
        return new OpenAPI()
                .info(new Info().
                        title("汽车美容店业务管理系统接口文档")
                        .version("1.0")
                        .description("汽车美容店业务管理系统接口文档")
                );
    }

    /**
     * 设置静态资源映射
     * @param registry 静态资源映射
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开始设置静态资源映射...");
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * 扩展Spring MVC框架的消息转化器
     * @param converters 消息转换器对象
     */
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("开始创建扩展消息转换器...");
        // 1、创建一个消息转换器对象
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        // 2、需要为消息转化器设置一个对象转换器，对象转换器可以将Java对象序列化为json数据
        converter.setObjectMapper(new JacksonObjectMapper());
        // 3、将自己的消息转换器加入到容器中（禁止使用最高优先级，不合适的index位置会导致文档请求异常）
        converters.add(converters.size()-2, converter);
    }
}
